﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using VBModel;

namespace VBBLL
{
    public class ApproveBll
    {


        public string ApproveBooking(string participants, string cc, string meetingTheme,
           string meetingAbstract, string phoneNo, string mtrAdd, int mtrNo, string specialRequestFirst,
               string specialRequestSecond, DateTime st, DateTime et, string startDate,
           string startTime, string endDate, string endTime, string userName, string weeks, string orgType1, string orgType3)
        {
            string ss = "";

            MTR_bas_BookingManage mtrBM = new MTR_bas_BookingManage();
            MTR_bas_BookingSchedul mtrBS = new MTR_bas_BookingSchedul();
            string keyId = Guid.NewGuid().ToString();
            var approveUrl = @"http://SZSPWEB02:2015/VenueBooking/WEB/ShowInfor.aspx?id=" + keyId + @"&isApprove=isApprove";
            //var approveUrl = @"http://workflow.kaifa.cn:2015/VenueBooking/WEB/ShowInfor.aspx?id=" + keyId + @"&isApprove=isApprove";
            string conn = WebConfigurationManager.ConnectionStrings["meetingroomManagementConnectionString"].ConnectionString;// "Data Source=mesdbsrv.gwkf.cn;Initial Catalog=VenueBooking;User ID=vbuser;Password=vb_2013";
            string strSql = string.Format(@"select [undefine3] from [VenueBooking].[dbo].[MTR_bas_Meetingroom] where mtrId ={0}", mtrNo);
            DataTable dt = Worker.GetDataSet(strSql, conn).Tables[0];
            string leadship = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                leadship += @"gwkf\" + dt.Rows[i][0].ToString() + ";";
            }
            string ApproveId = string.Empty;

            try
            {
                ApproveId = new BookBll().startApprove(userName, approveUrl, leadship);
            }
            catch (Exception e)
            {
                Worker.writeErrorMessage("0", e, "ApproveBooking");
                return "开启审批流失败" + e.Message;
            }
            string[] weekdays = weeks.Split(',');
            //单次预定审批
            if (weekdays.Length <= 1)
            {
                mtrBS.isPeriodic = false;
                mtrBM.bookingID = keyId;
                mtrBM.bookingType = "待审批";
                mtrBM.Meetingroom = mtrNo;
                mtrBM.bookingPerson = userName;
                mtrBM.bookingTime = DateTime.Now;  //保存预定时间
                mtrBM.meetingTheme = meetingTheme;
                mtrBM.participants = participants;
                mtrBM.meetingAbstract = meetingAbstract;
                mtrBM.specialRequestFirst = specialRequestFirst;
                mtrBM.specialRequestSecond = specialRequestSecond;
                mtrBM.bookingState = 1;
                mtrBM.undefine1 = orgType1;
                mtrBM.undefine2 = orgType3;
                mtrBM.useResult = 0;
                mtrBM.undefine3 = ApproveId;
                new BaseRepositoryBLL<MTR_bas_BookingManage>().AddEntities(mtrBM);
                mtrBS.Monday = false;
                mtrBS.Tuesday = false;
                mtrBS.Wednesday = false;
                mtrBS.Thursday = false;
                mtrBS.Friday = false;
                mtrBS.Saturday = false;
                mtrBS.Sunday = false;
                mtrBS.planId = keyId;
                mtrBS.meetingRoom = mtrNo;
                mtrBS.undefine2 = phoneNo;
                mtrBS.undefine3 = cc;
                mtrBS.meetingStartDate = Convert.ToDateTime(startDate);
                mtrBS.startTime = startTime;
                mtrBS.meetingEndDate = Convert.ToDateTime(endDate);
                mtrBS.endTime = endTime;
                mtrBS.undefine1 = new BookBll().timeDiffer((startDate + " " + mtrBS.startTime), (endDate + " " + mtrBS.endTime));
                new BaseRepositoryBLL<MTR_bas_BookingSchedul>().AddEntities(mtrBS);
                //mtr_BookingSuc.MTR_bas_BookingSchedul.Add(mtrBS);
                try
                {
                   
                    ss = "进入审批流程，请等待签批";
                }
                catch (Exception ex)
                {
                    Worker.writeErrorMessage("0", ex, "ApproveBooking1");
                    ss = ex.Message;
                }

            }
            return ss;
        }



    }
}
